package controllers

import (
	"read.com/admin/models"
)

type GroupController struct {
	BaseController
}

func (self *GroupController) List(){
	self.Data["pageTitle"] = "群发管理设置"
	appList := models.GetLinkageApp()
	self.Data["app"] = appList
	self.display()
}

func (self *GroupController) Table(){
	appId,_ := self.GetInt("appId")
	date := self.GetString("date")

	//列表参数
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 10
	}
	result,count := models.GetMassTaskList(appId,date,page,limit)
	self.ajaxList("成功", MSG_OK, count, result)
}

func (self *GroupController) Add(){
	appId,_ := self.GetInt("appId")
	sendDate := self.GetString("sendDate")
	sendTime,_ := self.GetInt8("sendTime")
	sendType,_ := self.GetInt8("sendType")
	headText := self.GetString("headText")
	tailText := self.GetString("tailText")
	err := models.AddTask(appId,sendDate,sendTime,sendType,headText,tailText)
	if err != nil {
		self.ajaxMsg("新增失败",MSG_ERR)
	}else{
		self.ajaxMsg("新增成功",MSG_OK)
	}
}

func (self *GroupController) Edit(){
	id,_ := self.GetInt("id")
	sendDate := self.GetString("sendDate")
	sendTime,_ := self.GetInt8("sendTime")
	sendType,_ := self.GetInt8("sendType")
	headText := self.GetString("headText")
	tailText := self.GetString("tailText")
	err := models.EditTask(id,sendDate,sendTime,sendType,headText,tailText)
	if err != nil {
		self.ajaxMsg("编辑失败",MSG_ERR)
	}else{
		self.ajaxMsg("编辑成功",MSG_OK)
	}
}

func (self *GroupController) Del(){
	id,_ := self.GetInt("id")

	err := models.DelTask(id)
	if err != nil {
		self.ajaxMsg("删除失败",MSG_ERR)
	}else{
		self.ajaxMsg("删除成功",MSG_OK)
	}
}

func (self *GroupController) Item(){
	taskId,_ := self.GetInt("taskId")
	isSend,_ := self.GetInt("isSend")
	appName := self.GetString("appName")
	self.Data["pageTitle"] = "任务条目管理"
	self.Data["taskId"] = taskId
	self.Data["isSend"] = isSend
	self.Data["appName"] = appName
	self.display()
}

func (self *GroupController) Itemtable(){
	taskId,_ := self.GetInt("taskId")
	appName := self.GetString("appName")
	//列表参数
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 10
	}
	result,count := models.GetItemList(appName,taskId,page,limit)
	self.ajaxList("成功", MSG_OK, count, result)
}

func (self *GroupController) AddItem(){
	taskId,_ := self.GetInt("taskId")
	tip := self.GetString("tip")
	data := self.GetString("data")
	err := models.AddTaskItem(taskId,tip,data)
	if err != nil {
		self.ajaxMsg("新增失败",MSG_ERR)
	}else{
		self.ajaxMsg("新增成功",MSG_OK)
	}
}

func (self *GroupController) EditItem(){
	id,_ := self.GetInt("id")
	tip := self.GetString("tip")
	data := self.GetString("data")

	err := models.EditTaskItem(id,tip,data)
	if err != nil {
		self.ajaxMsg("编辑失败",MSG_ERR)
	}else{
		self.ajaxMsg("编辑成功",MSG_OK)
	}
}

func (self *GroupController) DelItem(){
	id,_ := self.GetInt("id")

	err := models.DelTaskItem(id)
	if err != nil {
		self.ajaxMsg("删除失败",MSG_ERR)
	}else{
		self.ajaxMsg("删除成功",MSG_OK)
	}
}